<template>
  <div style='padding-bottom:60px;'>
    <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
      <!-- <van-cell v-for="item in list" :key="item" :title="item" /> -->
      <div class="list" v-for="(v,i) in listData" :key="i" @click="routerGo('/detail/'+v.id)">
        <div class="left">
          <img :src="v.image" alt />
        </div>
        <div class="right">
          <div class="title">{{v.name}}</div>
          <div class="detail">{{v.context}}</div>
        </div>
      </div>
    </van-list>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tag: 2,
      list: [],
      loading: false,
      finished: false,
      len: 10,
    };
  },
  computed: {
    listData() {
      // slice => 不修改原数组 = 返回一个新数组
      // splice => 修改原数组
      return this.$store.state.listData[this.tag].slice(0, this.len);
    }
  },
  created() {
    this.$http.get("/api/list?tag=" + this.tag).then(data => {
      this.$store.commit("setListData", {
        tag: this.tag,
        data: data.data
      });
    });
  },
  methods: {
    onLoad() {
      console.log('执行onLoad')
      // 异步更新数据
      this.loading = true;
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      setTimeout(() => {
        // for (let i = 0; i < 10; i++) {
        //   this.list.push(this.list.length + 1);
        // }
        this.len+=10
        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.len>= this.$store.state.listData[this.tag].length) {
          this.finished = true;
        }
      }, 1000);
    },
    routerGo(url) {
      this.$router.push(url);
    }
  }
};
</script>

<style lang='scss' scoped>
.list {
  width: 100%;
  padding: 20px;
  display: flex;
  .left {
    width: 100px;
    height: 100px;
  }
  .right {
    flex: 1;
    padding-left: 20px;
    .title {
      font-size: 16px;
      font-weight: bold;
      line-height: 40px;
    }
    .detail {
      font-size: 14px;
      width: calc(100vw - 160px);
      line-height: 30px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
</style>